package cn.shentianlan.controller;

import cn.shentianlan.criteria.CourseSchedulingCriteria;
import cn.shentianlan.domain.dto.CourseSchedulingBatchSaveDTO;
import cn.shentianlan.domain.dto.CourseSchedulingExportDTO;
import cn.shentianlan.domain.entity.CourseScheduling;
import cn.shentianlan.service.CourseSchedulingService;
import cn.shentianlan.utils.CourseSchedulingExcelManager;
import cn.shentianlan.utils.Result;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author 陈俊源
 * @Description 排课Controller
 * @Date 2021/5/1
 * @Version 1.0
 */
@RestController
@RequestMapping("/courseScheduling")
public class CourseSchedulingController {

    @Autowired
    private CourseSchedulingService courseSchedulingService;

    @Autowired
    private CourseSchedulingExcelManager courseSchedulingExcelManager;

    /**
     * 批量保存排课信息
     * @return
     */
    @RequestMapping("/batch")
    public Result batchSaveCourseScheduling(@RequestBody CourseSchedulingBatchSaveDTO courseSchedulingBatchSaveDTO){
        Result result = courseSchedulingService.batchSaveCourseScheduling(courseSchedulingBatchSaveDTO);
        return result;
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdateCourseScheduling(@RequestBody CourseScheduling courseScheduling){
        Result result = courseSchedulingService.saveOrUpdateCourseScheduling(courseScheduling);
        return result;
    }

    @PostMapping("/delete")
    public Result deleteCourseScheduling(Long id){
        Result result = courseSchedulingService.deleteCourseSchedulingById(id);
        return result;
    }

    /**
     * 查询所有排课信息
     * @param courseSchedulingCriteria
     * @return
     */
    @PostMapping("/list")
    public Result getCourseSchedulingList(@RequestBody CourseSchedulingCriteria courseSchedulingCriteria){
        Result result = courseSchedulingService.getCourseSchedulingList(courseSchedulingCriteria);
        return result;
    }

    /**
     * 查询当天有多少节课
     * @param courseSchedulingCriteria
     * @return
     */
    @PostMapping("/count")
    public Result getCourseSchedulingCourseCount(@RequestBody CourseSchedulingCriteria courseSchedulingCriteria){
        Result result = courseSchedulingService.getCourseSchedulingCourseCount(courseSchedulingCriteria);
        return result;
    }

    @PostMapping("/getCourseScheduling")
    public Result getCourseSchedulingById(Long id){
        Result result = courseSchedulingService.getCourseSchedulingById(id);
        return result;
    }

    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response, @Validated CourseSchedulingExportDTO dto) throws IOException {

        Workbook workbook = courseSchedulingExcelManager.createExcel(dto);

        String filename = (StringUtils.isEmpty(dto.getFilename()) ? "课表" : dto.getFilename()) + ".xlsx";
        response.reset();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), StandardCharsets.ISO_8859_1));
        workbook.write(response.getOutputStream());
        response.flushBuffer();
        workbook.close();
    }
}
